package com.franklin.java.action.sql.run;

import com.franklin.java.action.sql.AbstractSqlAction;
import com.franklin.java.action.sql.run.ui.RunSqlForm;
import com.franklin.java.icon.MybatisIcons;
import com.franklin.java.icon.PluginIcons;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.popup.*;
import com.intellij.openapi.util.NlsActions;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * 执行SQL Action（目标：兼容IC）
 * @author Franklin
 * @since 2021/9/22 10:14
 */
public class RunSqlAction extends AbstractSqlAction {
    public RunSqlAction(PsiElement sqlActionElement) {
        super(
            "Run this sql",
            "Helps you easier to test your complex xml sql",
            AllIcons.RunConfigurations.TestState.Run,
            sqlActionElement
        );
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        JBPopupFactory jbPopupFactory = JBPopupFactory.getInstance();

        RunSqlForm runSqlForm = new RunSqlForm(sqlActionElement);
        JPanel mainPanel = runSqlForm.getMainPanel();
        ComponentPopupBuilder componentPopupBuilder = jbPopupFactory.createComponentPopupBuilder(mainPanel, mainPanel);
        JBPopup runSqlPopup = componentPopupBuilder
            .setTitle("Run mybatis query sql")
            .setCancelKeyEnabled(true)
            .setCancelButton(new IconButton("close", AllIcons.Actions.CloseHovered))
            .setTitleIcon(new ActiveIcon(MybatisIcons.logo))
            .setMovable(true)
            .setShowShadow(true)
            .setResizable(true)
            .setRequestFocus(true)
            .setCancelOnClickOutside(false)
            .createPopup();

        runSqlPopup.setSize(new Dimension(1000,600));
        runSqlPopup.showCenteredInCurrentWindow(project);

        runSqlForm.getCancelMyButton().addActionListener(event -> runSqlPopup.cancel());
    }
}
